import * as path from "path";
import * as fse from "fs-extra";

import { IMAGE_DIR, OUTPUT_DIR, XML_DIR } from "./config";
import { isJPG, isXML, resolvePath, resolvePureName } from "./utils";

export function resolveImages() {
	const dir = resolvePath(IMAGE_DIR);
	const files = fse
		.readdirSync(dir)
		.filter((file) => isJPG(file))
		.map((file) => path.resolve(resolvePath(IMAGE_DIR), file));

	return files;
}

export function findXML(file: string) {
	const pureName = resolvePureName(file);
	const dir = resolvePath(XML_DIR);

	const target = fse
		.readdirSync(dir)
		.filter((file) => isXML(file))
		.filter((file) => resolvePureName(file) === pureName)
		.map((file) => path.resolve(resolvePath(XML_DIR), file));

	return target[0];
}

export function genOutputPath(file: string, newName?: string) {
	const tag = isJPG(file) ? "image" : "xml";
	return path.resolve(process.cwd(), OUTPUT_DIR, tag, newName ?? file);
}
